<template>
  <!-- 患者库 -->
  <div>
    <!-- 头部条件查询 - 开始 -->
    <div style="margin-top: 10px; font-weight: bold;float: left; width: 100%;">
      <el-form :inline="true" :model="queryTop" class="demo-form-inline" size="medium">
        <el-form-item label="患者姓名" :label-width="formLabelWidth">
          <el-input v-model="queryTop.name" placeholder="请输入患者姓名"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="queryTop.phone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" :label-width="formLabelWidth">
          <el-input v-model="queryTop.idCard" placeholder="请输入身份证号"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth">
          <el-button type="primary" size="small" icon="el-icon-search" @click="queryAll">搜索</el-button>
          <el-button type="primary" size="small" icon="el-icon-refresh" @click="chongzhi">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 头部条件查询 - 结束 -->

    <!-- 查看按钮 - 开始 -->
    <div style="margin-bottom:10px;float: left;">
      <el-button size="small" type="primary" icon="el-icon-plus" @click="archive">查看档案</el-button>
      <el-button size="small" type="success" icon="el-icon-edit" @click="casebook">查看就诊病例</el-button>
    </div>
    <!-- 查看按钮 - 结束 -->

    <!-- 全部患者列表 - 开始 -->
    <el-table ref="patientDataRef" :data="patientData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange" border :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="phone" label="电话" />
      <el-table-column prop="idCard" label="身份证号" />
      <el-table-column prop="birthday" label="出生年月" />
      <el-table-column prop="age" label="年龄" />
      <el-table-column prop="sex" label="性别" :formatter="sexFormat" />
      <el-table-column prop="isFinal" label="信息状态" :formatter="IsFinalFormat" />
      <el-table-column prop="createTime" label="创建时间" />
    </el-table>
    <!-- 全部患者列表 - 结束 -->

    <!-- 分页 - 开始 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page.current"
      :page-sizes="[5, 10, 15, 20]" :page-size="page.size" layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
    </el-pagination>
    <!-- 分页 - 结束 -->

    <!-- 档案弹出框 - 开始 -->
    <el-dialog title="查询患者档案信息" :visible.sync="Openarchive" width="60%" style="align-content: center;">
      <el-card class="box-card">
        <el-descriptions :data="patientAllArchive" class="margin-top" :column="2" size="medium">
          <el-descriptions-item>
            <template slot="label">
              ID
            </template>
            {{ patientAllArchive.patientId }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              姓名
            </template>
            {{ patientAllArchive.name }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              身份证号
            </template>
            {{ patientAllArchive.idCard }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              电话
            </template>
            {{ patientAllArchive.phone }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              患者性别
            </template>
            {{ patientAllArchive.sex }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              出生年月
            </template>
            {{ patientAllArchive.birthday }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              地址信息
            </template>
            {{ patientAllArchive.address }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              过敏信息
            </template>
            {{ patientAllArchive.allergyInfo }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              是否完善信息
            </template>
            {{ patientAllArchive.isFinal }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              最后登录IP
            </template>
            {{ patientAllArchive.lastLoginIp }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              最后登录时间
            </template>
            {{ patientAllArchive.lastLoginTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              创建时间
            </template>
            {{ patientAllArchive.createTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              更新时间
            </template>
            {{ patientAllArchive.updateTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              紧急联系人
            </template>
            {{ patientFileAllArchive.emergencyContactName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              紧急联系人电话
            </template>
            {{ patientFileAllArchive.emergencyContactPhone }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              关系
            </template>
            {{ patientFileAllArchive.emergencyContactRelation }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              左耳听力
            </template>
            {{ patientFileAllArchive.leftEarHearing }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              右耳听力
            </template>
            {{ patientFileAllArchive.rightEarHearing }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              左眼视力
            </template>
            {{ patientFileAllArchive.leftVision }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              右眼视力
            </template>
            {{ patientFileAllArchive.rightVision }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              身高
            </template>
            {{ patientFileAllArchive.height }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              体重
            </template>
            {{ patientFileAllArchive.weight }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              血型
            </template>
            {{ patientFileAllArchive.bloodType }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              个人史
            </template>
            {{ patientFileAllArchive.personalInfo }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              家族史
            </template>
            {{ patientFileAllArchive.familyInfo }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              档案创建时间
            </template>
            {{ patientFileAllArchive.createTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              档案更新时间
            </template>
            {{ patientFileAllArchive.updateTime }}
          </el-descriptions-item>
        </el-descriptions>
      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="Openarchive = false">取 消</el-button>
        <el-button type="primary" @click="Openarchive = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 档案弹出框 - 结束 -->

    <!-- 查询患者病例信息 - 开始 -->
    <el-dialog title="查询患者病例信息" :visible.sync="upCasebook" width="50%">
      <el-card class="box-card">
        <el-descriptions class="margin-top" :column="2" size="medium">
          <el-descriptions-item>
            <template slot="label">
              ID
            </template>
            {{ patientAllArchive.patientId }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              姓名
            </template>
            {{ patientAllArchive.name }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              身份证号
            </template>
            {{ patientAllArchive.idCard }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              电话
            </template>
            {{ patientAllArchive.phone }}
          </el-descriptions-item>
        </el-descriptions>
      </el-card>
      <el-card class="box-card" style="margin:5px 0px">
        <div v-if="careHistoryAasebook.length > 0">
          <el-collapse accordion>
            <el-collapse-item :name="item.chId"  v-for="item in careHistoryAasebook">
              <template slot="title">
                科室:【{{ item.deptName }}】就诊时间:【{{ item.careTime }}】
              </template>
              <div>主诉:{{ item.caseTitle }}</div>
              <div>诊断信息:{{ item.caseResult }}</div>
              <div>医生建议:{{ item.doctorTips }}</div>
              <div>备注:{{ item.remark }}</div>
              {{ item.careOrderChild }}
              <el-collapse v-if="item.careOrderList!==null" accordion>
                <el-collapse-item  :key="i"  v-for="(order,i) in item.careOrderList">
                  <template slot="title">
                    【 {{ order.coType === '0' ? '药品处方' : '检查处方' }}】【{{ i + 1 }}】 <span>【处方总额】:￥{{ order.allAmount}}
                    </span>
                  </template>
                  <el-table :data="order.careOrderItemList" border style="width: 100%"
                    :header-cell-style="{ 'text-align': 'center' }" :cell-style="{ 'text-align': 'center' }">
                    <el-table-column label="序号" align="center" type="index" width="50" />
                    <el-table-column :label="order.coType==='0'?'药品名称':'项目名称'" align="center" prop="itemName" />
                    <el-table-column label="数量" align="center" prop="num" />
                    <el-table-column label="单价(元)" align="center" prop="price" />
                    <el-table-column label="金额(元)" align="center" prop="amount" />
                    <el-table-column label="备注" prop="remark" align="center" />
                    <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter" />
                    </el-table>
                </el-collapse-item>
              </el-collapse>
            </el-collapse-item>
          </el-collapse>
        </div>
        <div v-else style="align-content: center;">
          暂无就诊数据
        </div>
      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="upCasebook = false">取 消</el-button>
        <el-button type="primary" @click="upCasebook = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 查询患者病例信息 - 结束 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 头部条件查询 - 条件
      queryTop: {
        name: '',
        phone: '',
        idCard: ''
      },
      formLabelWidth: '100px',
      // 按钮
      multipleSelection: [],

      // 全部患者列表
      patientData: [],
      // 信息状态
      isFinalOptions: [],
      // 性别状态
      sexOptions: [],
      // 分页
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      // 档案弹出框
      Openarchive: false,
      // 档案信息
      patientAllArchive: {},
      patientFileAllArchive: {},
      // 查询患者病例信息
      upCasebook: false,
      activeName: '1',
      // 就诊病例 药用处方信息&检查和用药项目明细
      careHistoryAasebook: [],
      registrationStatusOptions:[],
    }
  },
  created() {
    // 查询全部患者列表
    this.initpatientAll();
    // 信息状态查询
    this.initIsFinal();
    // 性别查询
    this.initSex();
// 挂号单类型查询
    this.initRegistrationStatus()
  },
  methods: {
    queryAll() {
      this.page.current = 1;
      this.initpatientAll();
    },
    // 重置
    chongzhi() {
      this.queryTop = {};
      this.page.current = 1;
      this.initpatientAll();
    },
    // 查看患者病例信息
    casebook() {
      if (this.multipleSelection.length == 1) {
        let patientId = this.multipleSelection[0].patientId;
        this.$axios.get("/doctor/api/patient/getArchiveById/" + patientId).then(result => {
          this.patientAllArchive = result.data.data;
        });
        this.$axios.get("/doctor/api/careHistory/getListByPatientId/" + patientId).then(result => {
          this.careHistoryAasebook = result.data.data;
        });
        this.upCasebook = true;
      } else if (this.multipleSelection.length > 1) {
        this.$message.error("只能勾选一位患者");
      } else {
        this.$message.error("请勾选一位患者");
      }

    },
    // 查看档案
    archive() {
      if (this.multipleSelection.length == 1) {
        let patientId = this.multipleSelection[0].patientId;
        this.$axios.get("/doctor/api/patient/getArchiveById/" + patientId).then(result => {
          this.patientAllArchive = result.data.data;
          this.patientAllArchive.sex = (this.patientAllArchive.sex === '0' ? '男' : this.patientAllArchive.sex === '1' ? '女' : '未知');
          this.patientAllArchive.isFinal = this.patientAllArchive.isFinal === '0' ? '未完善' : '已完善'
        });
        this.$axios.get("/doctor/api/patientFile/getArchiveById/" + patientId).then(result => {
          if (result.data.data !== null) {
            this.patientFileAllArchive = result.data.data;
          }
        });
        this.Openarchive = true;
      } else if (this.multipleSelection.length > 1) {
        this.$message.error("只能勾选一位患者");
      } else {
        this.$message.error("请勾选一位患者");
      }

    },

    // 查询全部患者列表
    initpatientAll() {
      this.$axios.post("/doctor/api/patient/patientPage/" + this.page.current + "/" + this.page.size, this.queryTop).then(result => {
        this.patientData = result.data.data.records;
        this.page.total = result.data.data.total;
      })
    },
    // 信息状态查询
    initIsFinal() {
      this.$axios.get("doctor/system/dictDataByType/his_patient_msg_final").then(row => {
        this.isFinalOptions = row.data.data;
      })
    },
    // 信息状态解析
    IsFinalFormat(row) {
      let v = row.isFinal;
      return this.formatDict(this.isFinalOptions, v);
    },
    // 挂号单类型查询
    initRegistrationStatus() {
      this.$axios.get("doctor/system/dictDataByType/his_registration_status").then(row => {
        this.registrationStatusOptions = row.data.data;
      })
    },
    // 挂号单类型解析
    statusFormatter(row) {
      let v = row.status;
      return this.formatDict(this.registrationStatusOptions, v);
    },
    // 性别查询
    initSex() {
      this.$axios.get("doctor/system/dictDataByType/sys_user_sex").then(row => {
        this.sexOptions = row.data.data;
      })
    },
    // 性别状态解析
    sexFormat(row) {
      let v = row.sex;
      return this.formatDict(this.sexOptions, v);
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 每页 ${val} 条
    handleSizeChange(val) {
      this.page.size = val
      this.initpatientAll();
    },
    //   当前页: ${val}
    handleCurrentChange(val) {
      this.page.current = val;
      this.initpatientAll();
    }
  },
}
</script>

<style>
bady {
  margin: 0%;
  padding: 0%;
}
</style>